package com.kui.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kui.entity.Qikan;
import com.kui.entity.User;
import com.kui.service.QikanService;

public class AddqikanServlet extends HttpServlet {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		
		
		if("member".equals(user.getRole())){
			response.sendRedirect("main.jspx");
			return;
		}
		
		request.getRequestDispatcher("WEB-INF/views/addqikan.jsp").forward(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String bianhao = request.getParameter("bianhao");
		String name = request.getParameter("name");
		
		
		if(bianhao == null || name == null || "".equals(bianhao) || "".equals(name)){
			response.sendRedirect("addqikan.jspx");
			return;
		}
			
			QikanService qs = new QikanService();
			
			Qikan q = qs.findById(bianhao);
			if(q != null){
				response.sendRedirect("addqikan.jspx");
				return;
			}else{
				Qikan qq = new Qikan();
				qq.setId(bianhao);
				qq.setName(name);
				qs.save(qq);
				response.sendRedirect("qikan.jspx");
			}
			
			
		
		
	}

}
